org.jgrapht.graph.Pseudograph Java Examples

The following examples show how to use org.jgrapht.graph.Pseudograph. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ContainersMapping.java    From powsybl-core with Mozilla Public License 2.0 6 votes vote down vote up
private static <N, B> void createVoltageLevelMapping(List<N> buses, List<B> branches, ToIntFunction<N> busToNum,
                                                     ToIntFunction<B> branchToNum1, ToIntFunction<B> branchToNum2,
                                                     ToDoubleFunction<B> branchToResistance, ToDoubleFunction<B> branchToReactance,
                                                     Function<Set<Integer>, String> busesToVoltageLevelId, ContainersMapping containersMapping) {
    UndirectedGraph<Integer, Object> vlGraph = new Pseudograph<>(Object.class);
    for (N bus : buses) {
        vlGraph.addVertex(busToNum.applyAsInt(bus));
    }
    for (B branch : branches) {
        if (branchToResistance.applyAsDouble(branch) == 0 && branchToReactance.applyAsDouble(branch) == 0) {
            vlGraph.addEdge(branchToNum1.applyAsInt(branch), branchToNum2.applyAsInt(branch));
        }
    }
    for (Set<Integer> busNums : new ConnectivityInspector<>(vlGraph).connectedSets()) {
        String voltageLevelId = busesToVoltageLevelId.apply(busNums);
        containersMapping.voltageLevelIdToBusNums.put(voltageLevelId, busNums);
        for (int busNum : busNums) {
            containersMapping.busNumToVoltageLevelId.put(busNum, voltageLevelId);
        }
    }
}
 
Example #2
Source File: SubstationIdMapping.java    From powsybl-core with Mozilla Public License 2.0 6 votes vote down vote up
private UndirectedGraph<String, Object> graphSubstationsTransformers() {
    UndirectedGraph<String, Object> graph = new Pseudograph<>(Object.class);
    for (PropertyBag s : context.cgmes().substations()) {
        String id = s.getId(CgmesNames.SUBSTATION);
        String iid = context.namingStrategy().getId(CgmesNames.SUBSTATION, id);
        graph.addVertex(iid);
    }
    for (PropertyBags tends : context.cgmes().groupedTransformerEnds().values()) {
        List<String> substationsIds = substationsIds(tends);
        if (substationsIds.size() > 1) {
            for (int i = 1; i < substationsIds.size(); i++) {
                graph.addEdge(substationsIds.get(0), substationsIds.get(i));
            }
        }
    }
    return graph;
}
 
Example #3
Source File: Graph.java    From PyramidShader with GNU General Public License v3.0 5 votes vote down vote up
public Graph(GeometryCollection lines) {
    graph = new Pseudograph<>(LineString.class);
    int nLines = lines.getNumGeometries();
    for (int i = 0; i < nLines; i++) {
        LineString line = (LineString) lines.getGeometryN(i);
        addEdge(line);
    }
    removeDegree2Nodes();
}
 
Example #4
Source File: AssociativeLogicUtils.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
public static Config buildConfig(String strategy, Pseudograph<String, LabeledEdge<String>> graph, Map<String, Map<String, String>> datasetToAssociations,
		List<SimpleFilter> selections, List<SimpleFilter> filters, Set<String> nearRealtimeDatasets, Map<String, Map<String, String>> datasetParameters,
		Set<String> documents) {
	Config config = new Config();
	config.setStrategy(strategy);
	config.setGraph(graph);
	config.setDatasetToAssociations(datasetToAssociations);
	config.setSelections(selections);
	config.setNearRealtimeDatasets(nearRealtimeDatasets);
	config.setDatasetParameters(datasetParameters);
	config.setDocuments(documents);
	config.setFilters(filters);
	return config;
}
 
Example #5
Source File: UcteNetworkExt.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
private UndirectedGraph<UcteNodeCode, Object> createSubstationGraph(UcteNetwork network) {
    UndirectedGraph<UcteNodeCode, Object> graph = new Pseudograph<>(Object.class);
    for (UcteNode node : network.getNodes()) {
        graph.addVertex(node.getCode());
    }

    // in the same substation...
    addEdgeBetweenSameGeographicalSpotNodes(network, graph);
    addEdgeBetweenTransformers(network, graph);
    addEdgeForCouplerOrLowImpedanceLine(network, graph);

    return graph;
}
 
Example #6
Source File: AssociativeLogicManagerTest.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void testProcessTwoDatasetsOneComplexAssociation() {
	Pseudograph<String, LabeledEdge<String>> graph = new Pseudograph<>(
			new ClassBasedEdgeFactory<String, LabeledEdge<String>>((Class<LabeledEdge<String>>) (Object) LabeledEdge.class));
	graph.addVertex(DS_STORE);
	graph.addVertex(DS_CUSTOMER);
	LabeledEdge<String> labeledEdgeStoreCustomer1 = new LabeledEdge<>(DS_STORE, DS_CUSTOMER, COL_CITY);
	graph.addEdge(DS_STORE, DS_CUSTOMER, labeledEdgeStoreCustomer1);
	LabeledEdge<String> labeledEdgeStoreCustomer2 = new LabeledEdge<>(DS_STORE, DS_CUSTOMER, COL_COUNTRY);
	graph.addEdge(DS_STORE, DS_CUSTOMER, labeledEdgeStoreCustomer2);

	Map<String, String> associationToColumnStore = new HashMap<>();
	associationToColumnStore.put(COL_CITY, COL_STORE_CITY);
	associationToColumnStore.put(COL_COUNTRY, COL_STORE_COUNTRY);
	Map<String, String> associationToColumnCustomer = new HashMap<>();
	associationToColumnCustomer.put(COL_CITY, COL_CITY);
	associationToColumnCustomer.put(COL_COUNTRY, COL_COUNTRY);
	Map<String, Map<String, String>> datasetToAssociations = new HashMap<>();
	datasetToAssociations.put(DS_STORE, associationToColumnStore);
	datasetToAssociations.put(DS_CUSTOMER, associationToColumnCustomer);

	List<SimpleFilter> selections = new ArrayList<>(1);
	IDataSet dataSet = dataSetDAO.loadDataSetByLabel(DS_CUSTOMER);
	selections.add(new InFilter(new Projection(dataSet, "gender"), "F"));

	Set<String> realtimeDatasets = new HashSet<>();
	Map<String, Map<String, String>> datasetParameters = new HashMap<>();
	Set<String> documents = new HashSet<>();

	Map<EdgeGroup, Set<Tuple>> edgeGroupToValues = null;

	Config config = AssociativeLogicUtils.buildConfig(AssociativeStrategyFactory.OUTER_STRATEGY, graph, datasetToAssociations, selections, realtimeDatasets,
			datasetParameters, documents);

	try {
		OuterAssociativityManager manager = new OuterAssociativityManager(config, UserProfileManager.getProfile());
		manager.process();
		edgeGroupToValues = manager.getResult().getEdgeGroupValues();
	} catch (Exception e) {
		e.printStackTrace();
		fail(e.toString());
	}

	assertNotNull(edgeGroupToValues);
	assertEquals(1, edgeGroupToValues.size());

	Set<LabeledEdge<String>> labeledEdges = new HashSet<>();
	labeledEdges.add(labeledEdgeStoreCustomer1);
	labeledEdges.add(labeledEdgeStoreCustomer2);
	EdgeGroup edgeGroup = new EdgeGroup(labeledEdges);
	assertTrue(edgeGroupToValues.containsKey(edgeGroup));

	Set<Tuple> values = edgeGroupToValues.get(edgeGroup);
	assertEquals(23, values.size());
}
 
Example #7
Source File: AssociativeLogicManagerTests.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void testProcessTwoDatasetsOneComplexAssociation() {
	Pseudograph<String, LabeledEdge<String>> graph = new Pseudograph<String, LabeledEdge<String>>(new ClassBasedEdgeFactory<String, LabeledEdge<String>>(
			(Class<LabeledEdge<String>>) (Object) LabeledEdge.class));
	graph.addVertex(DATASET_FOODMART_STORE);
	graph.addVertex(DATASET_FOODMART_CUSTOMER);
	LabeledEdge<String> labeledEdgeStoreCustomer1 = new LabeledEdge<String>(DATASET_FOODMART_STORE, DATASET_FOODMART_CUSTOMER, "A1");
	graph.addEdge(DATASET_FOODMART_STORE, DATASET_FOODMART_CUSTOMER, labeledEdgeStoreCustomer1);
	LabeledEdge<String> labeledEdgeStoreCustomer2 = new LabeledEdge<String>(DATASET_FOODMART_STORE, DATASET_FOODMART_CUSTOMER, "A2");
	graph.addEdge(DATASET_FOODMART_STORE, DATASET_FOODMART_CUSTOMER, labeledEdgeStoreCustomer2);

	Map<String, String> associationToColumnStore = new HashMap<String, String>();
	associationToColumnStore.put("A1", COLUMN_STORE_COUNTRY);
	associationToColumnStore.put("A2", COLUMN_STORE_CITY);
	Map<String, String> associationToColumnCustomer = new HashMap<String, String>();
	associationToColumnCustomer.put("A1", COLUMN_COUNTRY);
	associationToColumnCustomer.put("A2", COLUMN_CITY);
	Map<String, Map<String, String>> datasetToAssociations = new HashMap<String, Map<String, String>>();
	datasetToAssociations.put(DATASET_FOODMART_STORE, associationToColumnStore);
	datasetToAssociations.put(DATASET_FOODMART_CUSTOMER, associationToColumnCustomer);

	Map<String, String> selections = new HashMap<String, String>();
	selections.put(DATASET_FOODMART_CUSTOMER, "gender = 'F'");

	Set<String> realtimeDatasets = new HashSet<String>();
	Map<String, Map<String, String>> datasetParameters = new HashMap<String, Map<String, String>>();
	Set<String> documents = new HashSet<String>();

	Map<EdgeGroup, Set<String>> edgeGroupToValues = null;

	Config config = AssociativeLogicUtils.buildConfig(AssociativeStrategyFactory.OUTER_STRATEGY, graph, datasetToAssociations, selections,
			realtimeDatasets, datasetParameters, documents);

	try {
		OuterAssociativityManager manager = new OuterAssociativityManager(config, UserProfileManager.getProfile());
		edgeGroupToValues = manager.process().getEdgeGroupValues();
	} catch (Exception e) {
		e.printStackTrace();
		fail(e.toString());
	}

	assertNotNull(edgeGroupToValues);
	assertEquals(1, edgeGroupToValues.size());

	Set<LabeledEdge<String>> labeledEdges = new HashSet<LabeledEdge<String>>();
	labeledEdges.add(labeledEdgeStoreCustomer1);
	labeledEdges.add(labeledEdgeStoreCustomer2);
	EdgeGroup edgeGroup = new EdgeGroup(labeledEdges);
	assertTrue(edgeGroupToValues.containsKey(edgeGroup));

	Set<String> values = edgeGroupToValues.get(edgeGroup);
	assertEquals(109, values.size());
}
 
Example #8
Source File: AssociativeLogicManagerTests.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void testProcessFourDatasetsThreeSimpleAssociations() {
	Pseudograph<String, LabeledEdge<String>> graph = new Pseudograph<String, LabeledEdge<String>>(new ClassBasedEdgeFactory<String, LabeledEdge<String>>(
			(Class<LabeledEdge<String>>) (Object) LabeledEdge.class));
	graph.addVertex(DATASET_FOODMART_STORE);
	graph.addVertex(DATASET_FOODMART_CUSTOMER);
	graph.addVertex(DATASET_FOODMART_PRODUCT);
	graph.addVertex(DATASET_FOODMART_PRODUCTCLASS);
	LabeledEdge<String> labeledEdgeStoreCustomer = new LabeledEdge<String>(DATASET_FOODMART_STORE, DATASET_FOODMART_CUSTOMER, "A1");
	graph.addEdge(DATASET_FOODMART_STORE, DATASET_FOODMART_CUSTOMER, labeledEdgeStoreCustomer);
	LabeledEdge<String> labeledEdgeCustomerProduct = new LabeledEdge<String>(DATASET_FOODMART_CUSTOMER, DATASET_FOODMART_PRODUCT, "A2");
	graph.addEdge(DATASET_FOODMART_CUSTOMER, DATASET_FOODMART_PRODUCT, labeledEdgeCustomerProduct);
	LabeledEdge<String> labeledEdgeProductProductClass = new LabeledEdge<String>(DATASET_FOODMART_PRODUCT, DATASET_FOODMART_PRODUCTCLASS, "A3");
	graph.addEdge(DATASET_FOODMART_PRODUCT, DATASET_FOODMART_PRODUCTCLASS, labeledEdgeProductProductClass);

	Map<String, String> associationToColumnStore = new HashMap<String, String>();
	associationToColumnStore.put("A1", COLUMN_STORE_CITY);
	Map<String, String> associationToColumnProductClass = new HashMap<String, String>();
	associationToColumnProductClass.put("A3", COLUMN_PRODUCT_CLASS_ID);
	Map<String, String> associationToColumnProduct = new HashMap<String, String>();
	associationToColumnProduct.put("A2", COLUMN_PRODUCT_ID);
	associationToColumnProduct.put("A3", COLUMN_PRODUCT_CLASS_ID);
	Map<String, String> associationToColumnCustomer = new HashMap<String, String>();
	associationToColumnCustomer.put("A1", COLUMN_CITY);
	associationToColumnCustomer.put("A2", COLUMN_CUSTOMER_ID);
	Map<String, Map<String, String>> datasetToAssociations = new HashMap<String, Map<String, String>>();
	datasetToAssociations.put(DATASET_FOODMART_STORE, associationToColumnStore);
	datasetToAssociations.put(DATASET_FOODMART_PRODUCT, associationToColumnProduct);
	datasetToAssociations.put(DATASET_FOODMART_CUSTOMER, associationToColumnCustomer);
	datasetToAssociations.put(DATASET_FOODMART_PRODUCTCLASS, associationToColumnProductClass);

	Map<String, String> selections = new HashMap<String, String>();
	selections.put(DATASET_FOODMART_CUSTOMER, "account_num = '17993524670'");

	Set<String> realtimeDatasets = new HashSet<String>();
	Map<String, Map<String, String>> datasetParameters = new HashMap<String, Map<String, String>>();
	Set<String> documents = new HashSet<String>();

	Map<EdgeGroup, Set<String>> edgeGroupToValues = null;

	Config config = AssociativeLogicUtils.buildConfig(AssociativeStrategyFactory.OUTER_STRATEGY, graph, datasetToAssociations, selections,
			realtimeDatasets, datasetParameters, documents);

	try {
		OuterAssociativityManager manager = new OuterAssociativityManager(config, UserProfileManager.getProfile());
		edgeGroupToValues = manager.process().getEdgeGroupValues();
	} catch (Exception e) {
		e.printStackTrace();
		fail(e.toString());
	}

	assertNotNull(edgeGroupToValues);
	assertEquals(3, edgeGroupToValues.size());

	Set<LabeledEdge<String>> labeledEdges = new HashSet<LabeledEdge<String>>();

	labeledEdges.add(labeledEdgeCustomerProduct);
	EdgeGroup edgeGroup = new EdgeGroup(labeledEdges);
	assertTrue(edgeGroupToValues.containsKey(edgeGroup));
	Set<String> values = edgeGroupToValues.get(edgeGroup);
	assertEquals(1, values.size());
	assertTrue(values.contains("('2163')"));
	labeledEdges.clear();

	labeledEdges.add(labeledEdgeProductProductClass);
	edgeGroup = new EdgeGroup(labeledEdges);
	assertTrue(edgeGroupToValues.containsKey(edgeGroup));
	values = edgeGroupToValues.get(edgeGroup);
	assertTrue(values.isEmpty());
	labeledEdges.clear();

	labeledEdges.add(labeledEdgeStoreCustomer);
	edgeGroup = new EdgeGroup(labeledEdges);
	assertTrue(edgeGroupToValues.containsKey(edgeGroup));
	values = edgeGroupToValues.get(edgeGroup);
	assertTrue(values.contains("('Burnaby')"));
	labeledEdges.clear();
}
 
Example #9
Source File: AssociativeLogicManagerTests.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void testProcessThreeDatasetsTwoSimpleAssociationsTwoSelections() {
	Pseudograph<String, LabeledEdge<String>> graph = new Pseudograph<String, LabeledEdge<String>>(new ClassBasedEdgeFactory<String, LabeledEdge<String>>(
			(Class<LabeledEdge<String>>) (Object) LabeledEdge.class));
	graph.addVertex(DATASET_FOODMART_STORE);
	graph.addVertex(DATASET_FOODMART_CUSTOMER);
	graph.addVertex(DATASET_FOODMART_PRODUCT);
	LabeledEdge<String> labeledEdgeStoreCustomer = new LabeledEdge<String>(DATASET_FOODMART_STORE, DATASET_FOODMART_CUSTOMER, "A1");
	graph.addEdge(DATASET_FOODMART_STORE, DATASET_FOODMART_CUSTOMER, labeledEdgeStoreCustomer);
	LabeledEdge<String> labeledEdgeCustomerProduct = new LabeledEdge<String>(DATASET_FOODMART_CUSTOMER, DATASET_FOODMART_PRODUCT, "A2");
	graph.addEdge(DATASET_FOODMART_CUSTOMER, DATASET_FOODMART_PRODUCT, labeledEdgeCustomerProduct);

	Map<String, String> associationToColumnStore = new HashMap<String, String>();
	associationToColumnStore.put("A1", COLUMN_STORE_CITY);
	Map<String, String> associationToColumnProduct = new HashMap<String, String>();
	associationToColumnProduct.put("A2", COLUMN_PRODUCT_ID);
	Map<String, String> associationToColumnCustomer = new HashMap<String, String>();
	associationToColumnCustomer.put("A1", COLUMN_CITY);
	associationToColumnCustomer.put("A2", COLUMN_CUSTOMER_ID);
	Map<String, Map<String, String>> datasetToAssociations = new HashMap<String, Map<String, String>>();
	datasetToAssociations.put(DATASET_FOODMART_STORE, associationToColumnStore);
	datasetToAssociations.put(DATASET_FOODMART_PRODUCT, associationToColumnProduct);
	datasetToAssociations.put(DATASET_FOODMART_CUSTOMER, associationToColumnCustomer);

	Map<String, String> selections = new LinkedHashMap<String, String>();
	selections.put(DATASET_FOODMART_CUSTOMER, "account_num IN ('87500482201','17993524670')");
	selections.put(DATASET_FOODMART_PRODUCT, "product_id = '4'");

	Set<String> realtimeDatasets = new HashSet<String>();
	Map<String, Map<String, String>> datasetParameters = new HashMap<String, Map<String, String>>();
	Set<String> documents = new HashSet<String>();

	Map<EdgeGroup, Set<String>> edgeGroupToValues = null;

	Config config = AssociativeLogicUtils.buildConfig(AssociativeStrategyFactory.OUTER_STRATEGY, graph, datasetToAssociations, selections,
			realtimeDatasets, datasetParameters, documents);

	try {
		OuterAssociativityManager manager = new OuterAssociativityManager(config, UserProfileManager.getProfile());
		edgeGroupToValues = manager.process().getEdgeGroupValues();
	} catch (Exception e) {
		e.printStackTrace();
		fail(e.toString());
	}

	assertNotNull(edgeGroupToValues);
	assertEquals(2, edgeGroupToValues.size());

	Set<LabeledEdge<String>> labeledEdges = new HashSet<LabeledEdge<String>>();

	labeledEdges.add(labeledEdgeCustomerProduct);
	EdgeGroup edgeGroup = new EdgeGroup(labeledEdges);
	assertTrue(edgeGroupToValues.containsKey(edgeGroup));
	Set<String> values = edgeGroupToValues.get(edgeGroup);
	assertEquals(1, values.size());
	assertTrue(values.contains("('4')"));
	assertTrue(!values.contains("('2163')"));
	labeledEdges.clear();

	labeledEdges.add(labeledEdgeStoreCustomer);
	edgeGroup = new EdgeGroup(labeledEdges);
	assertTrue(edgeGroupToValues.containsKey(edgeGroup));
	values = edgeGroupToValues.get(edgeGroup);
	assertTrue(values.contains("('Burnaby')"));
}
 
Example #10
Source File: AssociativeLogicManagerTests.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void testProcessThreeDatasetsTwoSimpleAssociations() {
	Pseudograph<String, LabeledEdge<String>> graph = new Pseudograph<String, LabeledEdge<String>>(new ClassBasedEdgeFactory<String, LabeledEdge<String>>(
			(Class<LabeledEdge<String>>) (Object) LabeledEdge.class));
	graph.addVertex(DATASET_FOODMART_STORE);
	graph.addVertex(DATASET_FOODMART_CUSTOMER);
	graph.addVertex(DATASET_FOODMART_PRODUCT);
	LabeledEdge<String> labeledEdgeStoreCustomer = new LabeledEdge<String>(DATASET_FOODMART_STORE, DATASET_FOODMART_CUSTOMER, "A1");
	graph.addEdge(DATASET_FOODMART_STORE, DATASET_FOODMART_CUSTOMER, labeledEdgeStoreCustomer);
	LabeledEdge<String> labeledEdgeCustomerProduct = new LabeledEdge<String>(DATASET_FOODMART_CUSTOMER, DATASET_FOODMART_PRODUCT, "A2");
	graph.addEdge(DATASET_FOODMART_CUSTOMER, DATASET_FOODMART_PRODUCT, labeledEdgeCustomerProduct);

	Map<String, String> associationToColumnStore = new HashMap<String, String>();
	associationToColumnStore.put("A1", COLUMN_STORE_CITY);
	Map<String, String> associationToColumnProduct = new HashMap<String, String>();
	associationToColumnProduct.put("A2", COLUMN_PRODUCT_ID);
	Map<String, String> associationToColumnCustomer = new HashMap<String, String>();
	associationToColumnCustomer.put("A1", COLUMN_CITY);
	associationToColumnCustomer.put("A2", COLUMN_CUSTOMER_ID);
	Map<String, Map<String, String>> datasetToAssociations = new HashMap<String, Map<String, String>>();
	datasetToAssociations.put(DATASET_FOODMART_STORE, associationToColumnStore);
	datasetToAssociations.put(DATASET_FOODMART_PRODUCT, associationToColumnProduct);
	datasetToAssociations.put(DATASET_FOODMART_CUSTOMER, associationToColumnCustomer);

	Map<String, String> selections = new HashMap<String, String>();
	selections.put(DATASET_FOODMART_CUSTOMER, "account_num IN ('87500482201','17993524670')");

	Set<String> realtimeDatasets = new HashSet<String>();
	Map<String, Map<String, String>> datasetParameters = new HashMap<String, Map<String, String>>();
	Set<String> documents = new HashSet<String>();

	Map<EdgeGroup, Set<String>> edgeGroupToValues = null;

	Config config = AssociativeLogicUtils.buildConfig(AssociativeStrategyFactory.OUTER_STRATEGY, graph, datasetToAssociations, selections,
			realtimeDatasets, datasetParameters, documents);

	try {
		OuterAssociativityManager manager = new OuterAssociativityManager(config, UserProfileManager.getProfile());
		edgeGroupToValues = manager.process().getEdgeGroupValues();
	} catch (Exception e) {
		e.printStackTrace();
		fail(e.toString());
	}

	assertNotNull(edgeGroupToValues);
	assertEquals(2, edgeGroupToValues.size());

	Set<LabeledEdge<String>> labeledEdges = new HashSet<LabeledEdge<String>>();

	labeledEdges.add(labeledEdgeCustomerProduct);
	EdgeGroup edgeGroup = new EdgeGroup(labeledEdges);
	assertTrue(edgeGroupToValues.containsKey(edgeGroup));
	Set<String> values = edgeGroupToValues.get(edgeGroup);
	assertEquals(2, values.size());
	assertTrue(values.contains("('4')"));
	assertTrue(values.contains("('2163')"));
	labeledEdges.clear();

	labeledEdges.add(labeledEdgeStoreCustomer);
	edgeGroup = new EdgeGroup(labeledEdges);
	assertTrue(edgeGroupToValues.containsKey(edgeGroup));
	values = edgeGroupToValues.get(edgeGroup);
	assertTrue(values.contains("('Burnaby')"));
}
 
Example #11
Source File: AssociativeLogicManagerTests.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void testProcessTwoDatasetsOneSimpleAssociation() {
	Pseudograph<String, LabeledEdge<String>> graph = new Pseudograph<String, LabeledEdge<String>>(new ClassBasedEdgeFactory<String, LabeledEdge<String>>(
			(Class<LabeledEdge<String>>) (Object) LabeledEdge.class));
	graph.addVertex(DATASET_FOODMART_STORE);
	graph.addVertex(DATASET_FOODMART_CUSTOMER);
	LabeledEdge<String> labeledEdge = new LabeledEdge<String>(DATASET_FOODMART_STORE, DATASET_FOODMART_CUSTOMER, "A1");
	graph.addEdge(DATASET_FOODMART_STORE, DATASET_FOODMART_CUSTOMER, labeledEdge);

	Map<String, Map<String, String>> datasetToAssociations = new HashMap<String, Map<String, String>>();
	Map<String, String> associationToColumns = new HashMap<String, String>(1);
	associationToColumns.put("A1", COLUMN_STORE_CITY);
	datasetToAssociations.put(DATASET_FOODMART_STORE, associationToColumns);
	associationToColumns = new HashMap<String, String>(1);
	associationToColumns.put("A1", COLUMN_CITY);
	datasetToAssociations.put(DATASET_FOODMART_CUSTOMER, associationToColumns);

	Map<String, String> selections = new HashMap<String, String>(1);
	selections.put(DATASET_FOODMART_CUSTOMER, "customer_id = '4'");

	Set<String> realtimeDatasets = new HashSet<String>(0);
	Map<String, Map<String, String>> datasetParameters = new HashMap<String, Map<String, String>>();
	Set<String> documents = new HashSet<String>(0);

	Map<EdgeGroup, Set<String>> edgeGroupToValues = null;

	Config config = AssociativeLogicUtils.buildConfig(AssociativeStrategyFactory.OUTER_STRATEGY, graph, datasetToAssociations, selections,
			realtimeDatasets, datasetParameters, documents);
	try {
		OuterAssociativityManager manager = new OuterAssociativityManager(config, UserProfileManager.getProfile());
		edgeGroupToValues = manager.process().getEdgeGroupValues();
	} catch (Exception e) {
		e.printStackTrace();
		fail(e.toString());
	}

	assertNotNull(edgeGroupToValues);

	Set<LabeledEdge<String>> labeledEdges = new HashSet<LabeledEdge<String>>();
	labeledEdges.add(labeledEdge);
	EdgeGroup edgeGroup = new EdgeGroup(labeledEdges);
	assertTrue(edgeGroupToValues.containsKey(edgeGroup));

	Set<String> values = edgeGroupToValues.get(edgeGroup);
	assertEquals(1, values.size());
	assertTrue(values.contains("('Burnaby')"));
}
 
Example #12
Source File: AssociationAnalyzerTest.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void testGetSelections() {
	Set<Tuple> valuesA1A5 = new HashSet<>();
	valuesA1A5.add(new Tuple(Arrays.asList(744, 2275)));

	Set<Tuple> valuesA1A5A6 = new HashSet<>();
	valuesA1A5A6.add(new Tuple(Arrays.asList(744, 2275, 0)));

	Set<Tuple> valuesA3 = new HashSet<>();
	valuesA3.add(new Tuple(Arrays.asList(1)));

	Set<Tuple> valuesA4 = new HashSet<>();
	valuesA4.add(new Tuple(Arrays.asList(11)));

	Set<LabeledEdge<String>> edgesA1A5 = new HashSet<LabeledEdge<String>>();
	edgesA1A5.add(new LabeledEdge<String>(X, Y, A1));
	edgesA1A5.add(new LabeledEdge<String>(Y, X, A5));

	Set<LabeledEdge<String>> edgesA1A5A6 = new HashSet<LabeledEdge<String>>();
	edgesA1A5A6.add(new LabeledEdge<String>(X, Z, A1));
	edgesA1A5A6.add(new LabeledEdge<String>(Z, X, A5));
	edgesA1A5A6.add(new LabeledEdge<String>(X, Z, A6));

	Set<LabeledEdge<String>> edgesA3 = new HashSet<LabeledEdge<String>>();
	edgesA3.add(new LabeledEdge<String>(Z, K, A3));

	Set<LabeledEdge<String>> edgesA4 = new HashSet<LabeledEdge<String>>();
	edgesA4.add(new LabeledEdge<String>(Y, W, A4));

	EdgeGroup edgeGroupA1A5 = new EdgeGroup(edgesA1A5);
	EdgeGroup edgeGroupA1A5A6 = new EdgeGroup(edgesA1A5A6);
	EdgeGroup edgeGroupA3 = new EdgeGroup(edgesA3);
	EdgeGroup edgeGroupA4 = new EdgeGroup(edgesA4);

	// Map<EdgeGroup, Set<String>> egdegroupToValuesMap = new HashMap<EdgeGroup, Set<String>>();
	// egdegroupToValuesMap.put(edgeGroupA1A5, valuesA1A5);
	// egdegroupToValuesMap.put(edgeGroupA1A5A6, valuesA1A5A6);
	// egdegroupToValuesMap.put(edgeGroupA3, valuesA3);
	// egdegroupToValuesMap.put(edgeGroupA4, valuesA4);

	AssociativeLogicResult result = new AssociativeLogicResult();
	result.getEdgeGroupValues().put(edgeGroupA1A5, valuesA1A5);
	result.getEdgeGroupValues().put(edgeGroupA1A5A6, valuesA1A5A6);
	result.getEdgeGroupValues().put(edgeGroupA3, valuesA3);
	result.getEdgeGroupValues().put(edgeGroupA4, valuesA4);

	AssociationAnalyzer analyzer = new AssociationAnalyzer(fiveAssociationsMap.values());
	analyzer.process();
	Pseudograph<String, LabeledEdge<String>> graph = analyzer.getGraph();

	AssociationGroup associationGroup = new AssociationGroup();
	associationGroup.addAssociations(fiveAssociationsMap.values());
	Map<String, Map<String, Set<Tuple>>> selections = null;
	// selections = AssociationAnalyzer.getSelections(associationGroup, graph, result); // FIXME

	Map<String, Set<Tuple>> columnsToValuesMap = null;
	String columns = null;

	assertTrue(selections.containsKey(K));
	columnsToValuesMap = selections.get(K);
	columns = buildColumnLabel(K, A3);
	assertTrue(columnsToValuesMap.containsKey(columns));
	assertTrue(columnsToValuesMap.get(columns).equals(valuesA3));

	assertTrue(selections.containsKey(W));
	columnsToValuesMap = selections.get(W);
	columns = buildColumnLabel(W, A4);
	assertTrue(columnsToValuesMap.containsKey(columns));
	assertTrue(columnsToValuesMap.get(columns).equals(valuesA4));

	assertTrue(selections.containsKey(X));
	columnsToValuesMap = selections.get(X);
	columns = buildColumnLabel(X, A1) + "," + buildColumnLabel(X, A5) + "," + buildColumnLabel(X, A6);
	assertTrue(columnsToValuesMap.containsKey(columns));
	assertTrue(columnsToValuesMap.get(columns).equals(valuesA1A5A6));

	assertTrue(selections.containsKey(Y));
	columnsToValuesMap = selections.get(Y);
	columns = buildColumnLabel(Y, A1) + "," + buildColumnLabel(Y, A5);
	assertTrue(columnsToValuesMap.containsKey(columns));
	assertTrue(columnsToValuesMap.get(columns).equals(valuesA1A5));
	columns = buildColumnLabel(Y, A4);
	assertTrue(columnsToValuesMap.containsKey(columns));
	assertTrue(columnsToValuesMap.get(columns).equals(valuesA4));

	assertTrue(selections.containsKey(Z));
	columnsToValuesMap = selections.get(Z);
	columns = buildColumnLabel(Z, A1) + "," + buildColumnLabel(Z, A5) + "," + buildColumnLabel(Z, A6);
	assertTrue(columnsToValuesMap.containsKey(columns));
	assertTrue(columnsToValuesMap.get(columns).equals(valuesA1A5A6));
	columns = buildColumnLabel(Z, A3);
	assertTrue(columnsToValuesMap.containsKey(columns));
	assertTrue(columnsToValuesMap.get(columns).equals(valuesA3));
}
 
Example #13
Source File: AssociationAnalyzerTest.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void testProcessFiveAssociations() {
	AssociationAnalyzer analyzer = new AssociationAnalyzer(fiveAssociationsMap.values());
	analyzer.process();
	Map<String, Map<String, String>> datasetToAssociationToColumnMap = analyzer.getDatasetToAssociationToColumnMap();
	Pseudograph<String, LabeledEdge<String>> graph = analyzer.getGraph();

	assertTrue(datasetToAssociationToColumnMap.containsKey(K));
	assertTrue(datasetToAssociationToColumnMap.containsKey(W));
	assertTrue(datasetToAssociationToColumnMap.containsKey(X));
	assertTrue(datasetToAssociationToColumnMap.containsKey(Y));
	assertTrue(datasetToAssociationToColumnMap.containsKey(Z));

	Map<String, String> associationToColumnMap = null;

	associationToColumnMap = datasetToAssociationToColumnMap.get(K);
	assertTrue(associationToColumnMap.containsKey(A3));
	assertEquals(buildColumnLabel(K, A3), associationToColumnMap.get(A3));

	associationToColumnMap = datasetToAssociationToColumnMap.get(W);
	assertTrue(associationToColumnMap.containsKey(A4));
	assertEquals(buildColumnLabel(W, A4), associationToColumnMap.get(A4));

	associationToColumnMap = datasetToAssociationToColumnMap.get(X);
	assertTrue(associationToColumnMap.containsKey(A1));
	assertEquals(buildColumnLabel(X, A1), associationToColumnMap.get(A1));
	assertTrue(associationToColumnMap.containsKey(A5));
	assertEquals(buildColumnLabel(X, A5), associationToColumnMap.get(A5));
	assertTrue(associationToColumnMap.containsKey(A6));
	assertEquals(buildColumnLabel(X, A6), associationToColumnMap.get(A6));

	associationToColumnMap = datasetToAssociationToColumnMap.get(Y);
	assertTrue(associationToColumnMap.containsKey(A1));
	assertEquals(buildColumnLabel(Y, A1), associationToColumnMap.get(A1));
	assertTrue(associationToColumnMap.containsKey(A4));
	assertEquals(buildColumnLabel(Y, A4), associationToColumnMap.get(A4));
	assertTrue(associationToColumnMap.containsKey(A5));
	assertEquals(buildColumnLabel(Y, A5), associationToColumnMap.get(A5));

	associationToColumnMap = datasetToAssociationToColumnMap.get(Z);
	assertTrue(associationToColumnMap.containsKey(A1));
	assertEquals(buildColumnLabel(Z, A1), associationToColumnMap.get(A1));
	assertTrue(associationToColumnMap.containsKey(A5));
	assertEquals(buildColumnLabel(Z, A5), associationToColumnMap.get(A5));
	assertTrue(associationToColumnMap.containsKey(A6));
	assertEquals(buildColumnLabel(Z, A6), associationToColumnMap.get(A6));

	assertEquals(5, graph.vertexSet().size());
	assertTrue(graph.containsVertex(K));
	assertTrue(graph.containsVertex(W));
	assertTrue(graph.containsVertex(X));
	assertTrue(graph.containsVertex(Y));
	assertTrue(graph.containsVertex(Z));

	assertEquals(9, graph.edgeSet().size());
	assertTrue(graph.containsEdge(new LabeledEdge<String>(X, Y, A1)));
	assertTrue(graph.containsEdge(new LabeledEdge<String>(Y, Z, A1)));
	assertTrue(graph.containsEdge(new LabeledEdge<String>(Z, X, A1)));
	assertTrue(graph.containsEdge(new LabeledEdge<String>(K, Z, A3)));
	assertTrue(graph.containsEdge(new LabeledEdge<String>(W, Y, A4)));
	assertTrue(graph.containsEdge(new LabeledEdge<String>(X, Y, A5)));
	assertTrue(graph.containsEdge(new LabeledEdge<String>(Y, Z, A5)));
	assertTrue(graph.containsEdge(new LabeledEdge<String>(X, Z, A5)));
	assertTrue(graph.containsEdge(new LabeledEdge<String>(Z, X, A6)));
}
 
Example #14
Source File: Config.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public Pseudograph<String, LabeledEdge<String>> getGraph() {
	return graph;
}
 
Example #15
Source File: AssociativeLogicManagerTest.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void testProcessThreeDatasetsTwoSimpleAssociations() {
	Pseudograph<String, LabeledEdge<String>> graph = new Pseudograph<>(
			new ClassBasedEdgeFactory<String, LabeledEdge<String>>((Class<LabeledEdge<String>>) (Object) LabeledEdge.class));
	graph.addVertex(DS_STORE);
	graph.addVertex(DS_SALES_FACT_1997);
	graph.addVertex(DS_PRODUCT);
	LabeledEdge<String> labeledEdgeStoreSales = new LabeledEdge<>(DS_STORE, DS_SALES_FACT_1997, COL_STORE_ID);
	graph.addEdge(DS_STORE, DS_SALES_FACT_1997, labeledEdgeStoreSales);
	LabeledEdge<String> labeledEdgeSalesProduct = new LabeledEdge<>(DS_SALES_FACT_1997, DS_PRODUCT, COL_PRODUCT_ID);
	graph.addEdge(DS_SALES_FACT_1997, DS_PRODUCT, labeledEdgeSalesProduct);

	Map<String, String> associationToColumnStore = new HashMap<>();
	associationToColumnStore.put(COL_STORE_ID, COL_STORE_ID);
	Map<String, String> associationToColumnProduct = new HashMap<>();
	associationToColumnProduct.put(COL_PRODUCT_ID, COL_PRODUCT_ID);
	Map<String, String> associationToColumnSales = new HashMap<>();
	associationToColumnSales.put(COL_STORE_ID, COL_STORE_ID);
	associationToColumnSales.put(COL_PRODUCT_ID, COL_PRODUCT_ID);
	Map<String, Map<String, String>> datasetToAssociations = new HashMap<>();
	datasetToAssociations.put(DS_STORE, associationToColumnStore);
	datasetToAssociations.put(DS_PRODUCT, associationToColumnProduct);
	datasetToAssociations.put(DS_SALES_FACT_1997, associationToColumnSales);

	List<SimpleFilter> selections = new ArrayList<>(1);
	IDataSet dataSet = dataSetDAO.loadDataSetByLabel(DS_PRODUCT);
	selections.add(new InFilter(new Projection(dataSet, "brand_name"), "Queen"));

	Set<String> realtimeDatasets = new HashSet<>();
	realtimeDatasets.add(DS_STORE);
	realtimeDatasets.add(DS_SALES_FACT_1997);
	realtimeDatasets.add(DS_PRODUCT);

	Map<String, Map<String, String>> datasetParameters = new HashMap<>();
	HashMap<String, String> storeParameters = new HashMap<>();
	storeParameters.put("store_id", "100");
	datasetParameters.put(DS_STORE, storeParameters);

	Set<String> documents = new HashSet<>();

	Map<EdgeGroup, Set<Tuple>> edgeGroupToValues = null;

	Config config = AssociativeLogicUtils.buildConfig(AssociativeStrategyFactory.OUTER_STRATEGY, graph, datasetToAssociations, selections, realtimeDatasets,
			datasetParameters, documents);

	try {
		OuterAssociativityManager manager = new OuterAssociativityManager(config, UserProfileManager.getProfile());
		manager.process();
		edgeGroupToValues = manager.getResult().getEdgeGroupValues();
	} catch (Exception e) {
		e.printStackTrace();
		fail(e.toString());
	}

	assertNotNull(edgeGroupToValues);
	assertEquals(2, edgeGroupToValues.size());

	Set<LabeledEdge<String>> labeledEdges = new HashSet<>();
	labeledEdges.add(labeledEdgeSalesProduct);
	EdgeGroup edgeGroup = new EdgeGroup(labeledEdges);
	assertTrue(edgeGroupToValues.containsKey(edgeGroup));

	Set<Tuple> values = edgeGroupToValues.get(edgeGroup);
	assertEquals(2, values.size());
	assertTrue(values.contains("('41')"));
	assertTrue(values.contains("('42')"));

	labeledEdges.clear();
	labeledEdges.add(labeledEdgeStoreSales);
	edgeGroup = new EdgeGroup(labeledEdges);
	assertTrue(edgeGroupToValues.containsKey(edgeGroup));

	values = edgeGroupToValues.get(edgeGroup);
	assertEquals(22, values.size());
	assertTrue(!values.contains("('0')"));
	assertTrue(!values.contains("('2')"));
	assertTrue(!values.contains("('22')"));
}
 
Example #16
Source File: AssociativeLogicManagerTest.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void testProcessTwoDatasetsOneSimpleAssociation() {
	Pseudograph<String, LabeledEdge<String>> graph = new Pseudograph<>(
			new ClassBasedEdgeFactory<String, LabeledEdge<String>>((Class<LabeledEdge<String>>) (Object) LabeledEdge.class));
	graph.addVertex(DS_STORE);
	graph.addVertex(DS_SALES_FACT_1998);
	LabeledEdge<String> labeledEdge = new LabeledEdge<>(DS_STORE, DS_SALES_FACT_1998, COL_STORE_ID);
	graph.addEdge(DS_STORE, DS_SALES_FACT_1998, labeledEdge);

	Map<String, Map<String, String>> datasetToAssociations = new HashMap<>();
	Map<String, String> associationToColumns = new HashMap<>();
	associationToColumns.put(COL_STORE_ID, COL_STORE_ID);
	datasetToAssociations.put(DS_STORE, associationToColumns);
	datasetToAssociations.put(DS_SALES_FACT_1998, associationToColumns);

	List<SimpleFilter> selections = new ArrayList<>(1);
	IDataSet dataSet = dataSetDAO.loadDataSetByLabel(DS_STORE);
	selections.add(new InFilter(new Projection(dataSet, "store_type"), Arrays.asList(new String[]{"Small Grocery"})));

	// realtimes means that they are not cached
	Set<String> realtimeDatasets = new HashSet<>();
	realtimeDatasets.add(DS_STORE);
	realtimeDatasets.add(DS_SALES_FACT_1998);

	Map<String, Map<String, String>> datasetParameters = new HashMap<>();
	HashMap<String, String> storeParameters = new HashMap<>();
	storeParameters.put("store_id", "100");
	datasetParameters.put(DS_STORE, storeParameters);

	Set<String> documents = new HashSet<>();

	Map<EdgeGroup, Set<Tuple>> edgeGroupToValues = null;

	Config config = AssociativeLogicUtils.buildConfig(AssociativeStrategyFactory.OUTER_STRATEGY, graph, datasetToAssociations, selections, realtimeDatasets,
			datasetParameters, documents);

	try {
		OuterAssociativityManager manager = new OuterAssociativityManager(config, UserProfileManager.getProfile());
		manager.process();
		edgeGroupToValues = manager.getResult().getEdgeGroupValues();
	} catch (Exception e) {
		e.printStackTrace();
		fail(e.toString());
	}

	assertNotNull(edgeGroupToValues);

	Set<LabeledEdge<String>> labeledEdges = new HashSet<>();
	labeledEdges.add(labeledEdge);
	// edge group is the collection if relations (associations) between 2 datasets
	EdgeGroup edgeGroup = new EdgeGroup(labeledEdges);
	assertTrue(edgeGroupToValues.containsKey(edgeGroup));

	Set<Tuple> values = edgeGroupToValues.get(edgeGroup);
	assertEquals(4, values.size());
	assertTrue(values.contains(new Tuple(Arrays.asList(new Integer[]{2}))));
	assertTrue(values.contains(new Tuple(Arrays.asList(new Integer[]{5}))));
	assertTrue(values.contains(new Tuple(Arrays.asList(new Integer[]{14}))));
	assertTrue(values.contains(new Tuple(Arrays.asList(new Integer[]{22}))));
}
 
Example #17
Source File: AssociationAnalyzer.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public Pseudograph<String, LabeledEdge<String>> getGraph() {
	return graph;
}
 
Example #18
Source File: AssociationAnalyzer.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public AssociationAnalyzer(Collection<Association> associations) {
	this.associations = associations;
	this.datasetToAssociationToColumnMap = new HashMap<String, Map<String, String>>();
	this.graph = new Pseudograph<String, LabeledEdge<String>>(
			new ClassBasedEdgeFactory<String, LabeledEdge<String>>((Class<LabeledEdge<String>>) (Object) LabeledEdge.class));
}
 
Example #19
Source File: ModelBasedGraphGenerator.java    From JQF with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private Graph<Integer, DefaultEdge> createGraph() {
    Class<? extends DefaultEdge> edgeClass =
            model.weighted() ? DefaultWeightedEdge.class : DefaultEdge.class;

    if (model.loops()) {
        if (model.multiGraph() == false) {
            throw new IllegalArgumentException("Self-loops are only supported " +
                    "with multi-graphs");
        }
        if (isDirected()) {
            if (model.weighted()) {
                return new DirectedWeightedPseudograph<>(edgeClass);
            } else {
                return new DirectedPseudograph<>(edgeClass);
            }
        } else {
            if (model.weighted()) {
                return new WeightedPseudograph<>(edgeClass);
            } else {
                return new Pseudograph<>(edgeClass);
            }
        }
    } else {
        if (model.multiGraph()) {
            if (isDirected()) {
                if (model.weighted()) {
                    return new DirectedWeightedMultigraph<>(edgeClass);
                } else {
                    return new DirectedMultigraph<>(edgeClass);
                }
            } else {
                if (model.weighted()) {
                    return new WeightedMultigraph<>(edgeClass);
                } else {
                    return new Multigraph<>(edgeClass);
                }
            }
        } else {
            if (isDirected()) {
                if (model.weighted()) {
                    return new SimpleDirectedWeightedGraph<>(edgeClass);
                } else {
                    return new SimpleDirectedGraph<>(edgeClass);
                }
            } else {
                if (model.weighted()) {
                    return new SimpleWeightedGraph<>(edgeClass);
                } else {
                    return new SimpleGraph<>(edgeClass);
                }
            }
        }
    }

}
 
Example #20
Source File: AssociativeLogicUtils.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public static Config buildConfig(String strategy, Pseudograph<String, LabeledEdge<String>> graph, Map<String, Map<String, String>> datasetToAssociations,
		List<SimpleFilter> selections, Set<String> nearRealtimeDatasets, Map<String, Map<String, String>> datasetParameters, Set<String> documents) {
	return buildConfig(strategy, graph, datasetToAssociations, selections, new ArrayList<SimpleFilter>(), nearRealtimeDatasets, datasetParameters,
			documents);
}
 
Example #21
Source File: Config.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public void setGraph(Pseudograph<String, LabeledEdge<String>> graph) {
	this.graph = graph;
}